using UnityEngine;
using System.Collections;

/// \class  gkAbyssalGiantPickablePowerUpComponent
/// \brief  Make a actor int a pickable power up
[AddComponentMenu("GK/AbyssalGiant/PowerUp/gkAbyssalGiantPickablePowerUpComponent")]
public class gkAbyssalGiantPickablePowerUpComponent : MonoBehaviour 
{
	/// The power gain when taking this power up
	public float PowerGain = 0.1f;
	
	/// The power gain when taking this power up
	public float PickUpSpeed = 1.0f;
	
	/// The picker
	private GameObject m_rPicker; 
	
	/// \brief  Called just after the game begin to play
	void Start()
	{
		// Place the Pickup anim on an other layer
		animation["PowerUp_PickUp"].layer = 1;
	}
	
	/// \brief  Call at the pickup animation end
	public void OnPickUpAnimationEnd()
	{
		// Auto-destroy
		Destroy(gameObject);
	}
	
	/// \brief  Call at the pickup animation pick up instant
	public void OnPickUpAnimationPickUpInstant()
	{
		if(m_rPicker != null)
		{
			// Give the touched object some power
			m_rPicker.GetComponent<gkPowerComponent>().AddPower(PowerGain);
			
			// Notify it of the pick up
			m_rPicker.GetComponent<gkEventComponent>().RaiseEvent(gkAbyssalGiantPickUpEvent.PickUp);
		}
	}
		
	/// \brief  Call when actor pass through an other actoror
    void OnTriggerEnter(Collider a_rcollisionInfo)
    {
		if(a_rcollisionInfo.gameObject.name == "Submarine")
		{
			// Save the picker
			m_rPicker = a_rcollisionInfo.gameObject;
			
			// Pick up anim
			
			// Play the Pickup anim
			animation["PowerUp_PickUp"].speed = PickUpSpeed;
			animation.Play("PowerUp_PickUp");
			
			// Avoid base animation looping
			animation["PowerUp"].wrapMode = WrapMode.Once;
			animation["PowerUp"].time %= animation["PowerUp"].length;
		}
	}
}
